﻿package com.gengine.resource.core
{
    import __AS3__.vec.*;
    import com.gengine.core.frame.*;

    public class StreamManager extends Object
    {
        public static var defaultFrameLoadNum:int = 1;
        public static var frameLoadByteArrayNum:int;
        public static var frameLoadedNum:int = 0;
        public static var streamLoaderVect:Vector.<StreamLoader> = new Vector.<StreamLoader>;
        public static var timer:FrameTimer;
        public static var _isStart:Boolean = false;

        public function StreamManager()
        {
            return;
        }// end function

        public static function init() : void
        {
            if (!_isStart)
            {
                timer = new FrameTimer(1, int.MAX_VALUE, true);
                timer.addListener(TimerType.ENTERFRAME, onEnterFrame);
                timer.start();
                _isStart = true;
            }
            return;
        }// end function

        private static function onEnterFrame(param1:FrameTimer) : void
        {
            var _loc_2:StreamLoader = null;
            frameLoadedNum = 0;
            frameLoadByteArrayNum = Math.max(streamLoaderVect.length / 5, defaultFrameLoadNum);
            while (streamLoaderVect.length > 0 && frameLoadedNum < frameLoadByteArrayNum)
            {
                
                var _loc_4:* = frameLoadedNum + 1;
                frameLoadedNum = _loc_4;
                _loc_2 = streamLoaderVect.shift();
                loadStream(_loc_2);
            }
            return;
        }// end function

        private static function loadStream(param1:StreamLoader) : void
        {
            param1.loadByteArray();
            return;
        }// end function

        private static function removeOut(param1:StreamLoader) : void
        {
            var _loc_2:* = streamLoaderVect.indexOf(param1);
            if (_loc_2 >= 0)
            {
                streamLoaderVect.splice(_loc_2, 1);
            }
            return;
        }// end function

        public static function pushIn(param1:StreamLoader) : void
        {
            if (!_isStart)
            {
                loadStream(param1);
            }
            else if (frameLoadedNum < frameLoadByteArrayNum)
            {
                loadStream(param1);
                var _loc_3:* = frameLoadedNum + 1;
                frameLoadedNum = _loc_3;
            }
            else
            {
                streamLoaderVect.push(param1);
            }
            return;
        }// end function

    }
}
